from sqlalchemy import Column, Integer, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship

from db.model.base import Base


# 寄存器配置表
class Register(Base):
    __tablename__ = 'register'
    id = Column(Integer, primary_key=True)
    slave_id = Column(Integer, ForeignKey('slave.id'))
    register_type = Column(Integer, nullable=False,
                           comment="寄存器类型:1线圈寄存器,2离散输入寄存器,3保持寄存器,4离散输出寄存器")
    start_address = Column(Integer, nullable=False)
    end_address = Column(Integer, nullable=False)

    # 关联从站
    slave = relationship("Slave", back_populates="register")

    __table_args__ = (UniqueConstraint("slave_id", "register_type", name="uq_register"), {"comment": "寄存器表"})